package com.chensheng.controller;

import com.chensheng.exception.SellException;
import com.chensheng.form.CategoryForm;
import com.chensheng.model.ProductCategory;
import com.chensheng.model.ProductInfo;
import com.chensheng.service.ProductCategorySercvice;
import com.chensheng.util.KeyUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author:ChenSheng
 * @Description:
 * @Date: Created in 下午 20:23 2018/5/18 0018
 */
@Controller
@Slf4j
@RequestMapping("/seller/category")
public class SellerCategoryController {

    @Autowired
    private ProductCategorySercvice productCategorySercvice;

    @GetMapping("/list")
    public ModelAndView list(@RequestParam(value = "page", defaultValue = "1") Integer page,
                             @RequestParam(value = "size", defaultValue = "5") Integer size,
                             Map<String, Object> map){
        Integer start = (page - 1) * size;
        Integer offset = size;
        List<ProductCategory> productCategoryList = productCategorySercvice.findAll(start, offset);
        Integer totalCount = productCategorySercvice.count();
        totalCount = ((totalCount - 1) / size) + 1;

        map.put("productCategoryList", productCategoryList);
        map.put("currentPage", page);
        map.put("size", size);
        map.put("totalCount", totalCount);

        return new ModelAndView("category/list", map);
    }

    @GetMapping("/index")
    public ModelAndView index(@RequestParam(value="categoryId", required = false) Integer categoryId,
                              Map<String, Object> map){

        if(!StringUtils.isEmpty(categoryId)){
            ProductCategory productCategory = productCategorySercvice.findOne(categoryId);
            map.put("productCategory", productCategory);
        }
        return new ModelAndView("category/index", map);
    }

    @PostMapping("/save")
    public ModelAndView save(@Valid CategoryForm categoryForm,
                             BindingResult bindingResult,
                             Map<String, Object> map){
        if(bindingResult.hasErrors()){
            map.put("msg", bindingResult.getFieldError());
            map.put("url", "/seller/category/index");
            return new ModelAndView("common/error", map);
        }

        try{
            if(StringUtils.isEmpty(categoryForm.getCategoryId())){
                //插入一条数据
                ProductCategory productCategory = new ProductCategory();
                BeanUtils.copyProperties(categoryForm, productCategory);
                Date date = new Date();
                productCategory.setCreateTime(date);
                productCategory.setUpdateTime(date);

                productCategorySercvice.saveOne(productCategory);

            }else{
                //更新一条数据
                ProductCategory productCategory = productCategorySercvice.findOne(categoryForm.getCategoryId());
                BeanUtils.copyProperties(categoryForm, productCategory);
                productCategory.setUpdateTime(new Date());
                productCategorySercvice.updateOne(productCategory);
            }
        }catch(Exception e){
            map.put("msg", e.getMessage());
            map.put("url", "/seller/category/index");
            return new ModelAndView("common/error", map);
        }
        map.put("url", "/seller/category/list");
        return new ModelAndView("common/success");
    }

}
